/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InicioSesionPanel.java
 *
 * Created on 25-ene-2011, 18:46:33
 */

package Vista;

import Modelo.Entidades.Usuario;
import Modelo.SHA1;
import Vista.Sesion.SesionView;
import Vista.Sesion.SesionViewImpl;
import Vista.Usuario.UsuarioView;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import javax.swing.JOptionPane;

/*
 *  Proyecto eJAM [G12]
 *  Miembros:
 *      - Javier Ruiz Hidalgo
 *      - Adrian Ramos Cápitas
 *      - Miguel Alejandro Esteban Ordóñez
 */

public class InicioSesionPanel extends javax.swing.JPanel {

    private SesionViewImpl sesionViewImpl;
    private UsuarioView usuarioView;
    private VistaPrincipal vistaPrincipal;

    /** Creates new form InicioSesionPanel */
    public InicioSesionPanel(SesionView sesionView, UsuarioView usuarioView, VistaPrincipal vistaPrincipal) {
        this.sesionViewImpl = (SesionViewImpl)sesionView;
        this.vistaPrincipal = vistaPrincipal;
        this.usuarioView = usuarioView;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jDNI = new javax.swing.JTextField();
        jIdentificar = new javax.swing.JButton();
        jPassword = new javax.swing.JPasswordField();

        setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        setPreferredSize(new java.awt.Dimension(480, 50));

        jLabel1.setText("DNI: ");

        jLabel2.setText("Contraseña: ");

        jIdentificar.setText("Identificar");
        jIdentificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jIdentificarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDNI, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(12, 12, 12)
                .addComponent(jPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jIdentificar, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jDNI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jIdentificar))
                .addContainerGap(12, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jIdentificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jIdentificarActionPerformed
        Usuario usuario = null;

        if(this.usuarioView.getModel().compruebaUsuario(this.jDNI.getText()) == false){
            JOptionPane.showMessageDialog(null, "Error: Identificación Erronea");
        }
        else{
            usuario = this.usuarioView.getModel().obtenerEntidad(this.jDNI.getText());
            char [] claveInsertada = this.jPassword.getPassword();
            String clave = componeVectorPass(claveInsertada);
            String claveSHA1 = "";

            try {
                claveSHA1 = SHA1.getInstance().getHash(clave);
            } catch (NoSuchAlgorithmException ex) {}

            if(usuario.getPass().equals(claveSHA1)){
                this.sesionViewImpl.fireAltaSesionGesture(usuario, new Date());
                this.vistaPrincipal.refrescaVista();
                this.vistaPrincipal.añadePanelDesconexion();
            }
            else{
                JOptionPane.showMessageDialog(null, "Error: Identificación Erronea");
            }
        }
    }//GEN-LAST:event_jIdentificarActionPerformed

    private String componeVectorPass(char [] claveInsertada){
        String cad = "";
        
        for(int i = 0; i < claveInsertada.length; i++){
            cad += claveInsertada[i];
        }
        
        return cad;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jDNI;
    private javax.swing.JButton jIdentificar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField jPassword;
    // End of variables declaration//GEN-END:variables

}
